package com.cashmama.market.api.application.controller.authentication.voter;

import com.cashmama.market.api.application.controller.authentication.AuthenticationBaseHandler;
import com.cashmama.market.api.application.controller.authentication.AuthenticationController;
import com.cashmama.market.api.application.controller.authentication.handler.KycDocumentHandler;
import com.cashmama.market.api.application.controller.authentication.handler.vo.AadhaarCardSaveRequest;
import com.cashmama.market.api.application.controller.authentication.personalInfo.InputContent;
import com.cashmama.market.api.application.controller.authentication.response.UserProfileResponse;
import com.cashmama.market.api.application.controller.authentication.vos.AuthenticationContentVo;
import com.cashmama.market.api.application.controller.authentication.vos.InputParams;
import com.cashmama.market.api.application.enums.AuthenticationTypeEnum;
import com.cashmama.market.api.application.enums.ButtonTextEnum;
import com.cashmama.market.api.application.enums.ParamType;
import com.cashmama.market.api.domain.enums.KycTypeEnum;
import com.cashmama.market.api.domain.service.ApiUserService;
import com.cashmama.market.api.domain.vo.RequestLocalInfo;
import com.cashmama.market.api.infras.enums.JumpRouterEnum;
import com.cashmama.market.api.infras.utils.ParamsValidateUtil;
import com.qianli.user.enums.UserAuthEnum;
import com.qianli.user.enums.UserAuthStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA.
 * User: yujianfu (mr.vencnet@gmail.com)
 * Date: 2019-12-23
 * Time: 09:56
 */
@Service
public class VoterAuthenticationHandler extends AuthenticationBaseHandler {

    @Autowired
    private KycDocumentHandler kycDocumentHandler;
    @Autowired
    private ApiUserService apiUserService;

    @PostConstruct
    public void init() {
        AuthenticationController.authenticationBaseHandlerMap.put(AuthenticationTypeEnum.VOTER.getType(), this);
    }

    @Override
    public AuthenticationContentVo content() {
        AuthenticationContentVo authenticationContentVo = explain(new Voter());
        authenticationContentVo.setButtonText(ButtonTextEnum.SUBMIT.getText());
        authenticationContentVo.setButtonVisibility(true);
        return authenticationContentVo;
    }

    @Override
    public UserProfileResponse save(Map<String, String> inputParams) {
        Voter voter = new Voter();
        getObject(voter, inputParams);
        doSave(voter);
        UserProfileResponse userProfileResponse = new UserProfileResponse()
                .setJumpUrl(JumpRouterEnum.PERSONAL_INFO_BANK.getJumpUrl());
        return userProfileResponse;
    }

    private class Voter {
        @InputContent(paramName = "Card No.", param = "cardNo", inputDesc = "Enter your ID No.", paramType = ParamType.TEXT)
        private InputParams cardNo;

        @InputContent(paramName = "VOTER_ID(Front)", param = "frontImg", paramType = ParamType.POST_PHOTO)
        private InputParams frontImg;


        @InputContent(paramName = "VOTER_ID(Back)", param = "backImg", paramType = ParamType.POST_PHOTO)
        private InputParams backImg;

        @InputContent(paramName = "Selfie", param = "handHoldImg", paramType = ParamType.LIVE_CHECK)
        private InputParams handHoldImg;

        @InputContent(paramName = "Pancard No.", param = "pancardNo", paramType = ParamType.TEXT, inputDesc = "Enter your ID No.")
        private InputParams pancardNo;

        @InputContent(paramName = "Pancard No.", param = "pancardFrontImg", paramType = ParamType.POST_PHOTO)
        private InputParams pancardFrontImg;

        public InputParams getPancardNo() {
            return pancardNo;
        }

        public Voter setPancardNo(InputParams pancardNo) {
            this.pancardNo = pancardNo;
            return this;
        }

        public InputParams getPancardFrontImg() {
            return pancardFrontImg;
        }

        public Voter setPancardFrontImg(InputParams pancardFrontImg) {
            this.pancardFrontImg = pancardFrontImg;
            return this;
        }

        public InputParams getCardNo() {
            return cardNo;
        }

        public Voter setCardNo(InputParams cardNo) {
            this.cardNo = cardNo;
            return this;
        }

        public InputParams getFrontImg() {
            return frontImg;
        }

        public Voter setFrontImg(InputParams frontImg) {
            this.frontImg = frontImg;
            return this;
        }

        public InputParams getBackImg() {
            return backImg;
        }

        public Voter setBackImg(InputParams backImg) {
            this.backImg = backImg;
            return this;
        }

        public InputParams getHandHoldImg() {
            return handHoldImg;
        }

        public Voter setHandHoldImg(InputParams handHoldImg) {
            this.handHoldImg = handHoldImg;
            return this;
        }
    }

    private void doSave(Voter voter) {
        validate(voter);
        AadhaarCardSaveRequest request = new AadhaarCardSaveRequest()
                .setType(Integer.valueOf(KycTypeEnum.VOTER_ID.getType()))
                .setCardNo(voter.getCardNo().getInputValue())
                .setCardBackUrl(voter.getBackImg().getInputValue())
                .setCardFrontUrl(voter.getFrontImg().getInputValue())
                .setHandHeldUrl(voter.getHandHoldImg().getInputValue())
                .setPanCardNo(voter.getPancardNo().getInputValue())
                .setPanCardUrl(voter.getPancardFrontImg().getInputValue());
        kycDocumentHandler.kycDocumentHandler(request);
        apiUserService.updateUserAuthStatus(UserAuthStatusEnum.SUCCESS, UserAuthEnum.VOTER_STATUS, RequestLocalInfo.getCurrentUser().getUserCode());
        apiUserService.updateUserAuthStatus(UserAuthStatusEnum.SUCCESS, UserAuthEnum.PAN_STATUS, RequestLocalInfo.getCurrentUser().getUserCode());
    }

    private void validate(Voter voter){
        ParamsValidateUtil.checkParams(voter.getCardNo() == null, "CardNo can not be empty");
        ParamsValidateUtil.checkParams(voter.getBackImg() == null, "BackImg can not be empty");
        ParamsValidateUtil.checkParams(voter.getFrontImg() == null, "FrontImg can not be empty");
        ParamsValidateUtil.checkParams(voter.getHandHoldImg() == null, "HandHoldImg can not be empty");
        ParamsValidateUtil.checkParams(voter.getPancardNo() == null, "PancardNo can not be empty");
        ParamsValidateUtil.checkParams(voter.getPancardFrontImg() == null, "PancardFrontImg can not be empty");
    }

    public static void main(String[] args) {
        String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        Pattern regex = Pattern.compile(check);

        Matcher matcher = regex.matcher("123ssf asf@qq.com ".trim());
        System.out.println(matcher.matches());
        System.out.println("123ssf asf@qq.com ".replaceAll(" ", ""));
    }
}
